<?php
/**
 * Invision Power Board
 * Action controller for admin page
 */

class action_admin
{
	var $install;
	
	function action_admin( & $install )
	{
		$this->install =& $install;
	}
	
	function create_dir( $path )
	{
		mkdir($path); return true;
	}
	
	function makeTF($name)
	{
		$fh = @fopen( $name, "w" );
		@fwrite( $fh, '');
		@fclose( $fh );		
			return file_exists( $name ) ? 1 : 0;
	}
	
	function run()
	{
		/* Check input? */
		if( $this->install->mainClass->input['sub'] == 'check' )
		{			
			if( ! $this->install->mainClass->input['password'] )
			{
				$errors[] = 'You must specify a password for the admin account';	
			}
			else 
			{
				if( $this->install->mainClass->input['password'] != $this->install->mainClass->input['confirm_password']	)
				{
					$errors[] = 'The admin passwords did not match';	
				}
			}
			/* Check Directory */
			if( ! ( is_dir( $this->install->mainClass->input['home_dir'] ) ) )
			{
				$this->create_dir($this->install->mainClass->input[home_dir]);
			}
			
			if( ! ( is_dir( $this->install->mainClass->input['trash_dir'] ) ) ) {
			$this->create_dir($this->install->mainClass->input[trash_dir]);
			}
			
			/* Check URL */
			if( ! $this->install->mainClass->input['trash_dir'] )
			{
				$errors[] = 'You did not specify a trash directory';
			}

			if( ! ( file_exists ( $this->install->mainClass->input['trash_file'] ) ) )
			{
				if(! $this->makeTF( $this->install->mainClass->input['trash_file'] ) )
				{
					$errors[] = 'The specified location for the trash file does not exist!';
				}
			}
						
			if( is_array( $errors ) )
			{
				$this->install->template->warning( $errors );	
			}
			else 
			{
				/* Save Form Data */
				$this->install->saved_data['pass_main']  = $this->install->mainClass->input['password'];
				$this->install->saved_data['username'] = $this->install->mainClass->input['user'];
				$this->install->saved_data['home_dir'] = preg_replace( "#(//)$#", "", str_replace( '\\', '/', $this->install->mainClass->input['home_dir'] ) );
				$this->install->saved_data['trash_dir'] = preg_replace( "#(//)$#", "", str_replace( '\\', '/', $this->install->mainClass->input['trash_dir'] ) );
				$this->install->saved_data['trash_file'] = preg_replace( "#(//)$#", "", str_replace( '\\', '/', $this->install->mainClass->input['trash_file'] ) ) ;

				/* Next Action */
				$this->install->template->page_current = 'install';
				require_once( INS_ROOT_PATH . 'core/actions/install.php' );	
				$action = new action_install( &$this->install );
				$action->run();
				return;				
			}		
		}
		
		/* Guess at directory */
		$dir = str_replace( 'installer', '' , getcwd() );
		$dir = str_replace( 'install'  , '' , getcwd() );
		$dir = str_replace( 'oPanel/'  , '' , $dir );
		$dir = str_replace( '\\'       , '/', $dir );

		/* Output */
		$this->install->template->append( $this->install->template->admin_page( $dir, $trash, $trashfile ) );		
		$this->install->template->next_action = '?p=admin&sub=check';
	}
}

?>